package kr.saeildamil.androidstudy_second.network;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DownImage extends Activity {
	Button downBtn;
	Button btnEnQueue;
	ImageView imageView;
	DownloadManager mDm;
	long mId=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mDm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		downBtn = new Button(this);
		downBtn.setText("DownLoad Image");
		downBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				(new DownThread("http://img.gnj.kr/newsen/news_photo/2013/08/23/201308230828532510_1.jpg")).start();
			}
		});
		
		btnEnQueue = new Button(this);
		btnEnQueue.setText("다운로드 예약");
		btnEnQueue.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Uri uri = Uri.parse("http://developer.android.com/images/home/design.png");
				DownloadManager.Request req = new DownloadManager.Request(uri);
				req.setTitle("테스트 다운로드");
				req.setDescription("이미지 파일을 다운로드 받습니다");
				req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
				
				mId = mDm.enqueue(req);
				
				//다운로드메니저에서 BR 방송을 수신
				IntentFilter filter = new IntentFilter();
				filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
				registerReceiver(receiver, filter);
			}
		});
		
		imageView = new ImageView(this);
		
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		layout.addView(downBtn, params);
		layout.addView(imageView, params);
		
		setContentView(layout);
	}
	
	protected void onPause() {
		if (mId != 0){
			unregisterReceiver(receiver);
			mId = 0;
		}
	};
	
	BroadcastReceiver receiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(DownImage.this, "다운로드 완료", Toast.LENGTH_LONG).show();
		}
	};
	
	
	class DownThread extends Thread{
		String mAddr;

		public DownThread(String mAddr) {
			super();
			this.mAddr = mAddr;
		}
		
		public void run() {
			super.run();
			InputStream is = null;
			try {
				is = new URL(mAddr).openStream();
				Bitmap bit = BitmapFactory.decodeStream(is);
				Message msg = mAfterDown.obtainMessage();
				msg.obj = bit;
				mAfterDown.sendMessage(msg);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {is.close();} catch(Exception e){}
			}
		}
	}
	
	
	Handler mAfterDown = new Handler(){
		public void handleMessage(Message msg) {

			Bitmap bit = (Bitmap) msg.obj;
			if (bit == null){
				Toast.makeText(DownImage.this, "bitmap is null", Toast.LENGTH_LONG).show();
			}else{
				imageView.setImageBitmap(bit);
			}
		}
		
	};
}//end of Activity


